package com.sfj.common.util;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ThreadFactory;

public class ThreadFactoryUtil {

    static final Logger log = LoggerFactory.getLogger(ThreadFactoryUtil.class);

    public static ThreadFactory build(String threadName) {
        return new ThreadFactoryBuilder()
                .setNameFormat(threadName + "-%d")
                .setUncaughtExceptionHandler((t, e) ->
                        log.error("Exception occurred in the thread  {}  Exception : {}", t.getName(),
                                ExceptionUtils.getStackTrace(e)))
                .build();
    }
}
